package com.dyz.wsw.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * Redis的配置类
 * @author dyz
 * Time 2024/7/3 12:33
 */
@Configuration
@Slf4j
public class RedisConfig {

    public RedisConfig(){
        log.debug("创建配置类对象：RedisConfig");
    }

    @Bean
    public ReactiveRedisTemplate<String, Object> reactiveRedisTemplate(ReactiveRedisConnectionFactory factory){
        RedisSerializationContext.RedisSerializationContextBuilder<String, Object> builder = RedisSerializationContext.newSerializationContext();

        //配置序列化器
        builder.key(RedisSerializer.string());
        builder.value(RedisSerializer.json());
        builder.hashKey(RedisSerializer.string());
        builder.hashValue(RedisSerializer.json());

        return new ReactiveRedisTemplate<>(factory,builder.build());

    }
}
